﻿@inject DialogService DialogService
@inject IProductService ProductService
@inject ICustomerService CustomerService

<RadzenStack>
    <RadzenTemplateForm EditContext="editContext" @ref="form1" TItem="CustomerDto" Data="@Model" Submit=@OnSubmit InvalidSubmit=@OnInvalidSubmit>
        <RadzenFieldset Text="客户信息">
            <RadzenStack Gap="1rem">
                <RadzenStack class="rz-p-0"
                Orientation="Orientation.Vertical"
                AlignItems="AlignItems.Normal"
                JustifyContent="JustifyContent.SpaceBetween">
                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="姓名" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox Name="Name" @bind-Value="@Model!.Name" />
                                        <RadzenRequiredValidator Component="Name" Text="姓名不能为空。" Popup=true Style="position: absolute" />
                                    </ChildContent>
                                    <Helper>
                                        <RadzenText TextStyle="TextStyle.Caption" Style="color:red">
                                            * 必填
                                        </RadzenText>
                                    </Helper>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="手机号" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@Model!.Phone" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="地址" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextBox @bind-Value="@Model!.Address" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                    <RadzenRow Gap="1rem">
                        <RadzenColumn Size="12" SizeMD="12">
                            <RadzenStack>
                                <RadzenFormField Text="备注" Style="width: 100%;">
                                    <ChildContent>
                                        <RadzenTextArea @bind-Value="@Model!.Remark" />
                                    </ChildContent>
                                </RadzenFormField>
                            </RadzenStack>
                        </RadzenColumn>
                    </RadzenRow>

                </RadzenStack>

                <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
                    <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
                    <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存" ButtonType="ButtonType.Submit" />
                </RadzenStack>
            </RadzenStack>
        </RadzenFieldset>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    [Parameter] public CustomerDto? PCustomer { get; set; } 

    private EditContext? editContext;
    private CustomerDto? Model{ get; set; }
    RadzenTemplateForm<CustomerDto>? form1;

    async void OnSubmit(CustomerDto model)
    {
        try
        {
            await CustomerService.Save(model);
            DialogService.Close(model);
        }
        catch (Exception e)
        {
            await DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    void OnInvalidSubmit(FormInvalidSubmitEventArgs args)
    { 
        foreach (var error in args.Errors)
        {
            Console.WriteLine(error);
        }
    }

    private void OnCancel()
    {
        DialogService.Close(null);
    }

    protected override void OnInitialized()
    {
        Model = PCustomer!;
        editContext = new EditContext(Model);
    }
}


